package com.ihg.hiex.admin.web.controller.data;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.ihg.hiex.admin.service.DataService;
import com.ihg.hiex.entity.data.notice.EmailTemplate;

/**
 * 
 * @author houtao
 *
 */
@Controller
@RequestMapping("/email-templates")
@MenuSetting("menu-settings")
public class EmailTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(EmailTemplateController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<EmailTemplate> emailTemplateService;
    
    /**
     * list all the emailTemplate
     * 
     * @return
     */
    @MenuSetting("submenu-email-template-management")
    @RequestMapping(value = "" , method = RequestMethod.GET)
    public ModelAndView list() {
        
        ModelAndView modelAndView = new ModelAndView("settings/email-template/email-template-list");
        
        List<EmailTemplate> emailTemplates = emailTemplateService.listAll(EmailTemplate.class);
        modelAndView.addObject("emailTemplates", emailTemplates);
        
        return modelAndView;
        
    }
    
    /**
     * display edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit" , method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("settings/email-template/email-template-edit");
        
        EmailTemplate emailTemplate = emailTemplateService.load(EmailTemplate.class, id);
        
        modelAndView.addObject("emailTemplate", emailTemplate);
        
        return modelAndView;
        
    }
    
    /**
     * edit an emailTemplate
     * 
     * @param emailTemplate
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Preloading("id") EmailTemplate emailTemplate) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            emailTemplateService.update(emailTemplate);
            
            httpResult.success().message("修改邮件模版成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("修改邮件模版失败");
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
}
